package view.customer;

import com.jfoenix.controls.JFXCheckBox;
import dao.OrderDao;
import dao.UserDao;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.util.Callback;
import pojo.order.BidEnitity;
import pojo.order.BidProperty;
import view.Main;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
 * @author Limohan
 */
public class BidManageController {

    private Main myApp ;
    private static String UserId;
    private static String OrderId;
    private static List<BidEnitity> bidEnitities  = new ArrayList<>() ;
    private static OrderDao orderDao = new OrderDao();

    public String getOrderId() {
        return OrderId;
    }


    public void setOrderId(String orderId) {
        OrderId = orderId;
    }

    public  String getUserId() {
        return UserId;
    }

    public void setUserId(String userId) {
        UserId = userId;
    }

    public void setApp(Main myApp){
        this.myApp = myApp;
    }

    @FXML
    private TableView<BidProperty> bidTable;

    @FXML
    private TableColumn<BidProperty,Void> option;

    @FXML
    private TableColumn<BidProperty,String> idCol;

    @FXML
    private TableColumn<BidProperty,String> factoryCol;

    @FXML
    private TableColumn<BidProperty,String> factoryManager;

    @FXML
    private TableColumn<BidProperty,String> phone;

    @FXML
    private TableColumn<BidProperty,String> price;

    @FXML
    private TableColumn<BidProperty,String> isSuccess;

    @FXML
    void chooseBid(ActionEvent event) throws IOException {
        orderDao.chooseBid(bidEnitities.get(0),getOrderId());
        this.showAllInfo();
    }

    @FXML
    void lastPage(ActionEvent event) throws IOException {
        myApp.goToCustomerOrderPage();
    }

    public void showAllInfo() throws IOException {
        List<BidEnitity> bidEnitityList = orderDao.findOrder(OrderId).getBids();
        this.displayInfo(bidEnitityList);
    }

    private void addButtonToTable( TableColumn<BidProperty, Void> column){
        Callback<TableColumn<BidProperty, Void>, TableCell<BidProperty, Void>> cellFactory = new Callback<TableColumn<BidProperty, Void>, TableCell<BidProperty, Void>>() {
            @Override
            public TableCell<BidProperty, Void> call(final TableColumn<BidProperty, Void> param) {
                final TableCell<BidProperty, Void> cell = new TableCell<>() {

                    final JFXCheckBox btn = new JFXCheckBox();

                    {
                        btn.setOnAction((ActionEvent event) -> {
                            BidProperty bidProperty = getTableView().getItems().get(getIndex());
                            try {
                                bidEnitities.add(orderDao.findBidByFactory(getOrderId(),bidProperty.getFactory()));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        });
                    }

                    @Override
                    public void updateItem(Void item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setGraphic(null);
                        } else {
                            setGraphic(btn);
                        }
                    }
                };
                return cell;
            }
        };

        column.setCellFactory(cellFactory);

    }
    void displayInfo(List<BidEnitity> bids) throws IOException {
        bidTable.getItems();
        ObservableList<BidProperty> bidList = FXCollections.observableArrayList();
        if(bids!=null){
            for (BidEnitity bidEnitity : bids) {
                BidProperty bidProperty = new BidProperty();
                bidProperty.setId(bidEnitity.getId());
                bidProperty.setPrice(bidEnitity.getPrice());
                bidProperty.setFactory(bidEnitity.getFactory());
                bidProperty.setIsChosen(bidEnitity.getIsChosen());
                bidProperty.setName(bidEnitity.getName());
                bidProperty.setPhone(bidEnitity.getPhone());
                bidList.add(bidProperty);
            }
            addButtonToTable(option);
            idCol.setCellValueFactory(param -> param.getValue().idProperty());
            price.setCellValueFactory(param -> param.getValue().priceProperty());
            factoryCol.setCellValueFactory(param -> param.getValue().factoryProperty());
            isSuccess.setCellValueFactory(param -> param.getValue().isChosenProperty());
            factoryManager.setCellValueFactory(param -> param.getValue().nameProperty());
            phone.setCellValueFactory(param -> param.getValue().phoneProperty());
            bidTable.setItems(bidList);

        }else{
            bidTable.setItems(FXCollections.emptyObservableList());
        }
    }

}
